package com.infore.airsupervise.utils;

import com.infore.airsupervise.app.StatusConst;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 *
 * @author: dingxinlin
 * @date: 2016-11-24 18:39
 */

public class DateUtil {

    /**
     * 日期格式
     */

    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_TIME = " HH:mm:ss";
    public static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_DATE_HOUR = "yyyy-MM-dd HH";
    public static final String FORMAT_DATE_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    public static String dateFormatYM = "yyyy-MM";

    public static final long TIME_DAY = 24*60*60*1000;
    public static final long TIME_HOUR = 60*60*1000;

    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(FORMAT_DATE_TIME);
        }
    };

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(FORMAT_DATE);
        }
    };

    /**
     * 描述：Date类型转化为String类型.
     *
     * @param date   the date
     * @param format the format
     * @return String String类型日期时间
     */
    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        String strDate = null;
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    /**
     * 构建日期
     */
    public static String buildStrDate(int year, int month, int day, int hour, int minute, int second) {
        Date date = new Date(year - 1900, month - 1, day, hour, minute, second);
        return dateFormater.get().format(date);
    }

    public static String buildStrDate(int year, int month, int day) {
        return buildStrDate(year, month, day, 0, 0, 0);
    }

    /**
     * 获取当前日期
     * @param isFullDate
     * @return
     */
    public static String getCurrentDateStr(boolean isFullDate) {
        Calendar cal = Calendar.getInstance();
        String curDate = null;
        if (isFullDate) {
            curDate = dateFormater.get().format(cal.getTime());
        } else {
            curDate = dateFormater2.get().format(cal.getTime());
        }
        return curDate;
    }

    public static String getCurrentDateStr(String formatStr) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
        return dateFormat.format(cal.getTime());
    }

    /**
     * 将字符串转为日期类型
     *
     * @param sdate
     * @return
     */
    public static Date toDate(String sdate) {
        try {
            return dateFormater.get().parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date stringToDate(String sdate , String dateFormat){
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormat);
        try {
            return  mSimpleDateFormat.parse(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null ;
        }

    }

    /**
     * 日期比较
     * @param date1
     * @param date2
     * @return (毫秒)
     */
    public static long compareDate(String date1, String date2) {
        long space = toDate(date1).getTime() - toDate(date2).getTime();
        return space;
    }

    public static long compareDate(Date date1, Date date2) {
        long space = date1.getTime() - date2.getTime();
        return space;
    }

    public static long compareDate(String date1, String date2 , String dateFormat){
        long timeDis = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        try {
            timeDis = simpleDateFormat.parse(date2).getTime()-simpleDateFormat.parse(date1).getTime() ;
        } catch (ParseException e) {
            e.printStackTrace();
        }finally {
            return timeDis;
        }
    }

    /**
     * 拼接成长日期
     * @param shortDate
     * @return
     */
    public static String toLongDate(String shortDate) {
        if (shortDate.length() != 10) {
            return shortDate;
        }
        return shortDate + " 00:00:00";
    }

    /**
     * 截取为短日期
     * @param longDate
     * @return
     */
    public static String toShortDate(String longDate) {
        if (longDate.length() != 19) {
            return longDate;
        }
        return longDate.substring(0, 10);
    }

    /**
     * 加减一段时间
     */
    public static String updateTime(String mDate,int type , boolean isAdd){
        Date date;
        long m;
            try {
                if(type== StatusConst.STATISTIC_DAY){
                date = dateFormater2.get().parse(mDate);
                    m = isAdd ? date.getTime()+TIME_DAY : date.getTime()-TIME_DAY;
                    date.setTime(m);
                    return dateFormater2.get().format(date);
                }else {
                    date = dateFormater.get().parse(mDate+":00:00");
                    m=isAdd ? date.getTime()+TIME_HOUR : date.getTime()-TIME_HOUR;
                    date.setTime(m);
                    return dateFormater.get().format(date).substring(0,13);
                }
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
    }

    public static String getStringByOffset(String date, String format, int calendarField, int offset){
        String strDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = new GregorianCalendar();
        try {
            calendar.setTime(sdf.parse(date));
            calendar.add(calendarField,offset);
            strDate = sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strDate ;
    }

    public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            c.setTime(date);
            c.add(calendarField, offset);
            strDate = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
}
